.TH std::collate 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::collate \- std::collate

.SH Synopsis
   Defined in header <locale>
   template< class CharT >
   class collate;

   Class std::collate encapsulates locale-specific collation (comparison) and hashing
   of strings. This facet is used by std::basic_regex and can be applied, by means of
   std::locale::operator(), directly to all standard algorithms that expect a string
   comparison predicate.

   std-collate-inheritance.svg

                                   Inheritance diagram

.SH Specializations

   The standard library is guaranteed to provide the following specializations (they
   are required to be implemented by any locale object):

   Defined in header <locale>
   std::collate<char>    implements lexicographical ordering of byte strings
   std::collate<wchar_t> implements lexicographical ordering of wide strings

.SH Member types

   Member type Definition
   char_type   CharT
   string_type std::basic_string<CharT>

.SH Member functions

   constructor   constructs a new collate facet
                 \fI(public member function)\fP
   destructor    destructs a collate facet
                 \fI(protected member function)\fP
   compare       invokes do_compare
                 \fI(public member function)\fP
   transform     invokes do_transform
                 \fI(public member function)\fP
   hash          invokes do_hash
                 \fI(public member function)\fP

.SH Member objects

   static std::locale::id id id of the locale
                             \fI(public member object)\fP

.SH Protected member functions

   do_compare   compares two strings using this facet's collation rules
   \fB[virtual]\fP    \fI(virtual protected member function)\fP
   do_transform transforms a string so that collation can be replaced by comparison
   \fB[virtual]\fP    \fI(virtual protected member function)\fP
   do_hash      generates an integer hash value using this facet's collation rules
   \fB[virtual]\fP    \fI(virtual protected member function)\fP

.SH Example


// Run this code

 #include <algorithm>
 #include <iostream>
 #include <locale>
 #include <string>
 #include <vector>

 int main()
 {
     std::locale::global(std::locale("en_US.utf8"));
     std::wcout.imbue(std::locale(""));
     std::vector<std::wstring> v{
         L"ar", L"zebra", L"\\u00f6grupp", L"Zebra",
         L"\\u00e4ngel",L"\\u00e5r", L"f\\u00f6rnamn"
     };

     std::wcout << "Default locale collation order: ";
     std::sort(v.begin(), v.end());
     for (auto s : v)
         std::wcout << s << ' ';
     std::wcout << '\\n';

     std::wcout << "English locale collation order: ";
     std::sort(v.begin(), v.end(), std::locale("en_US.UTF-8"));
     for (auto s : v)
         std::wcout << s << ' ';
     std::wcout << '\\n';

     std::wcout << "Swedish locale collation order: ";
     std::sort(v.begin(), v.end(), std::locale("sv_SE.UTF-8"));
     for (auto s : v)
         std::wcout << s << ' ';
     std::wcout << '\\n';
 }

.SH Output:

 Default locale collation order: Zebra ar förnamn zebra ängel år ögrupp
 English locale collation order: ängel ar år förnamn ögrupp zebra Zebra
 Swedish locale collation order: ar förnamn zebra Zebra år ängel ögrupp

.SH See also

                  lexicographically compares two strings using this locale's collate
   operator()     facet
                  \fI(public member function of std::locale)\fP
   collate_byname represents the system-supplied std::collate for the named locale
                  \fI(class template)\fP
